package com.example.demo.controller.surperController;

import com.baomidou.mybatisplus.core.conditions.query.LambdaQueryWrapper;
import com.baomidou.mybatisplus.core.toolkit.Wrappers;
import com.baomidou.mybatisplus.extension.plugins.pagination.Page;
import com.example.demo.Mapper.ReadFeedbackMapper;
import com.example.demo.Mapper.unReadFeedbackMapper;
import com.example.demo.base.Result;
import com.example.demo.entity.ReadFeedback;
import com.example.demo.entity.unReadFeedback;
import org.springframework.web.bind.annotation.*;

import javax.annotation.Resource;
import java.util.ArrayList;
import java.util.List;

@RestController
@RequestMapping("/unReadFeedbackSurper")
public class unReadFeedbackSurperController {
    @Resource
    unReadFeedbackMapper unreadFeedbackMapper;
    @Resource
    ReadFeedbackMapper readFeedbackMapper;

    /**
     * 查询全部信息
     *
     * @return
     */
    @GetMapping("/all")
    public Result<?> findAll() {
        return Result.success(unreadFeedbackMapper.selectList(null));
    }

    /**
     * 分页查询未读反馈信息中所有数据
     *
     * @param pageNum
     * @param pageSize
     * @return
     */

    @GetMapping(value = "/pageAll")
    public Result<?> findPageU(@RequestParam(defaultValue = "1") Integer pageNum,
                               @RequestParam(defaultValue = "10") Integer pageSize) {
        LambdaQueryWrapper<unReadFeedback> wrapper = Wrappers.<unReadFeedback>lambdaQuery();
        Page<unReadFeedback> unReadFeedbackPage = unreadFeedbackMapper.selectPage(new Page<>(pageNum, pageSize), wrapper);
        return Result.success(unReadFeedbackPage);
    }

    /**
     * 新增
     *
     * @param name
     * @param sex
     * @param identify
     * @param tel
     * @param email
     * @param address
     * @param productCode
     * @param productName
     * @param feedbackInfo
     * @return
     */
    @GetMapping(value = "/add")
    public Result<?> saveU(@RequestParam String name,
                           @RequestParam String sex,
                           @RequestParam String identify,
                           @RequestParam String tel,
                           @RequestParam String email,
                           @RequestParam String address,
                           @RequestParam String productCode,
                           @RequestParam String productName,
                           @RequestParam String feedbackInfo,
                           @RequestParam String tenantId) {
        unReadFeedback unRead = new unReadFeedback();
        unRead.setName(name);
        unRead.setSex(sex);
        unRead.setIdentify(identify);
        unRead.setTel(tel);
        unRead.setEmail(email);
        unRead.setAddress(address);
        unRead.setProductCode(productCode);
        unRead.setProductName(productName);
        unRead.setFeedbackInfo(feedbackInfo);
        if (name.isEmpty() || sex.isEmpty() || identify.isEmpty() || tel.isEmpty() || email.isEmpty() || address.isEmpty() || productCode.isEmpty() || productName.isEmpty()) {
            return Result.error(0, "文本不能为空");
        }
        unreadFeedbackMapper.insert(unRead);
        return Result.success();
    }

    /**
     * 根据identify更新
     *
     * @param name
     * @param sex
     * @param identify
     * @param tel
     * @param email
     * @param address
     * @param productCode
     * @param productName
     * @param feedbackInfo
     * @return
     */

    @GetMapping(value = "/update")
    public Result<?> updateU(@RequestParam String name,
                             @RequestParam String sex,
                             @RequestParam String identify,
                             @RequestParam String tel,
                             @RequestParam String email,
                             @RequestParam String address,
                             @RequestParam String productCode,
                             @RequestParam String productName,
                             @RequestParam String feedbackInfo,
                             @RequestParam String tenantId) {
        unReadFeedback unRead = new unReadFeedback();
        unRead.setName(name);
        unRead.setSex(sex);
        unRead.setIdentify(identify);
        unRead.setTel(tel);
        unRead.setEmail(email);
        unRead.setAddress(address);
        unRead.setProductCode(productCode);
        unRead.setProductName(productName);
        unRead.setFeedbackInfo(feedbackInfo);
        if (name.isEmpty() || sex.isEmpty() || identify.isEmpty() || tel.isEmpty() || email.isEmpty() || address.isEmpty() || productCode.isEmpty() || productName.isEmpty()) {
            return Result.error(0, "文本不能为空");
        }
        unreadFeedbackMapper.updateById(unRead);
        return Result.success();
    }

    /**
     * 根据身份证查询信息
     *
     * @param identify
     * @return
     */
    @GetMapping("/findById/{identify}")
    public Result<?> findByFarmId(@PathVariable String identify) {
        unReadFeedback unRead = unreadFeedbackMapper.selectById(identify);
        List<unReadFeedback> unReadFeedbackInfoList = new ArrayList<>();
        if (unRead != null) {
            unReadFeedbackInfoList.add(unRead);
            return Result.success(unReadFeedbackInfoList);
        }
        return Result.error(0, "没有查询到相关数据");
    }

    /**
     * 根据name模糊查询
     *
     * @param name
     * @return
     */
    @GetMapping("/findByName/{name}")
    public Result<?> findByName(@PathVariable String name) {
        LambdaQueryWrapper<unReadFeedback> wrapper = Wrappers.<unReadFeedback>lambdaQuery();
        wrapper.like(unReadFeedback::getName, name);
        List<unReadFeedback> unReadFeedbackList = unreadFeedbackMapper.selectList(wrapper);
        if (unReadFeedbackList.size() > 0)
            return Result.success(unReadFeedbackList);
        return Result.error(0, "没有查询到相关数据");
    }

    /**
     * 根据身份证删除信息
     *
     * @param identify
     * @return
     */

    @DeleteMapping("/delById/{identify}")
    public Result<?> delete(@PathVariable String identify) {
        int res = unreadFeedbackMapper.deleteById(identify);
        if (res > 0)
            return Result.success();
        return Result.error(0, "删除失败");
    }

    /**
     * 根据id来将信息读取并插入已读表
     *
     * @param identify
     * @return
     */
    @GetMapping("/read/{identify}")
    public Result<?> read(@PathVariable String identify) {
        unReadFeedback unread = unreadFeedbackMapper.selectById(identify);
        ReadFeedback read = new ReadFeedback();
        read.setName(unread.getName());
        read.setSex(unread.getSex());
        read.setIdentify(unread.getIdentify());
        read.setTel(unread.getTel());
        read.setEmail(unread.getEmail());
        read.setAddress(unread.getAddress());
        read.setProductCode(unread.getProductCode());
        read.setProductName(unread.getProductName());
        read.setFeedbackInfo(unread.getFeedbackInfo());
        readFeedbackMapper.insert(read);
        unreadFeedbackMapper.deleteById(identify);
        return Result.success();
    }

}

